package com.inhim.pjhim.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.CollectionList;
import com.inhim.pjhim.utils.ImageLoaderUtils;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import fm.jiecao.jcvideoplayer_lib.JCUtils;


public class CollectionRadioAdapter extends RecyclerArrayAdapter<CollectionList.List> {

    Context context;
    private boolean isCheck,isAll,mIsCheck;
    private int mPosition;
    public CollectionRadioAdapter(Context context){
        super(context);
        this.context = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new PersonViewHolder(parent,viewType);
    }
    public void setCheck(boolean isCheck,boolean isAll){
        this.isCheck=isCheck;
        this.isAll=isAll;
    }

    public void setCheck(boolean isCheck,int position) {
        this.mIsCheck=isCheck;
        this.mPosition=position;
    }

    private boolean isCheck(){
        return isCheck;
    }
    private boolean isAll(){
        return isAll;
    }
    public class PersonViewHolder extends BaseViewHolder<CollectionList.List> {

        ImageView iv_title;
        TextView tv_title;
        TextView tv_num1;
        TextView tv_time1;
        CheckBox checkbox,iv_icon;
        TextView tv_lable;
        TextView tv_time;
        int position;
        PersonViewHolder(ViewGroup parent,int position){
            super(parent, R.layout.item_collection_radio);
            iv_title = getView(R.id.iv_title1);
            tv_title = getView(R.id.tv_title1);
            tv_num1 = getView(R.id.tv_num1);
            tv_time1 = getView(R.id.tv_time1);
            checkbox=getView(R.id.checkbox);
            iv_icon=getView(R.id.iv_icon);
            tv_lable=getView(R.id.tv_lable);
            tv_time=getView(R.id.tv_time);
            position=position;
            //设置子View的点击事件
            addOnClickListener(R.id.checkbox);
            addOnClickListener(R.id.iv_icon);
        }
        @Override
        public void setData(final CollectionList.List data) {
            super.setData(data);
            if(data.getStatus()!=null&&data.getStatus().equals("0")){
                iv_icon.setVisibility(View.GONE);
                tv_lable.setVisibility(View.GONE);
                tv_title.setText("此数据已被管理员删除");
                iv_title.setImageResource(R.mipmap.readerinfo_delete);
                tv_time.setVisibility(View.GONE);
            }else{
                tv_lable.setVisibility(View.VISIBLE);
                tv_time.setVisibility(View.VISIBLE);
                if(data.getReaderEntity()!=null && data.getReaderEntity().getTagName()!=null){
                    tv_lable.setText(data.getReaderEntity().getTagName());
                }
                if(data.getReaderEntity()!=null){
                    ImageLoaderUtils.setImage(data.getReaderEntity().getCover(), iv_title);
                    tv_num1.setText(data.getReaderEntity().getReadAmount());
                    tv_title.setText(data.getReaderEntity().getTitle());
                    if(data.getReaderEntity().getAuthor()!=null){
                        tv_num1.setText(data.getReaderEntity().getAuthor().getName());
                    }
                    if(data.getReaderEntity().getDuration()!=null){
                        tv_time.setText(JCUtils.stringForTime2(Integer.valueOf(data.getReaderEntity().getDuration())));
                    }
                }
                if(data.getTimeText()!=null){
                    tv_time1.setText(data.getTimeText());
                }
                //1文章 2视频 3音频
            }
            if(isCheck()){
                checkbox.setVisibility(View.VISIBLE);
            }else{
                //checkbox.setVisibility(View.GONE);
            }
            if(isAll()){
                checkbox.setChecked(true);
            }else{
                checkbox.setChecked(false);
            }
            if(mPosition==position){
                iv_icon.setChecked(mIsCheck);
            }else{
                iv_icon.setChecked(false);
            }
        }
    }
}
